/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.hibernate.HibernateException;
import persistencia.EmpleadosDAO;
import persistencia.ProveedoresDAO;
import persistencia.TipoEmpleadoDAO;
import pojos.Asigna;
import pojos.AsignaId;
import pojos.Empleados;
import pojos.Modulopermisosempleados;
import pojos.Permisosempleados;
import pojos.Tipoempleado;

/**
 *
 * @author Alejandrolpz
 */
public class Jd_modificar_empleado extends javax.swing.JDialog {
    
    Empleados emple;
    Permisosempleados permiso;
    Modulopermisosempleados modulo;
    
    /**
     * Creates new form JDialog
     */
    public Jd_modificar_empleado(JPanel parent, Empleados emple) {
        initComponents();
        this.permiso=null;
        this.modulo=null;
        this.emple=emple;
        Object[] tiposEmple=new TipoEmpleadoDAO().getTiposEmpleado().toArray();
        this.cbTipoEmple.setModel(new DefaultComboBoxModel(tiposEmple));
        this.tfDNI.setText(emple.getDni());
        this.tfNombre.setText(emple.getNombre());
        this.tfApellido1.setText(emple.getApellido1());
        this.tfApellido2.setText(emple.getApellido2());
        this.tfDir.setText(emple.getDireccion());
        this.tfCP.setText(""+emple.getCp());
        this.tfCCC.setText(emple.getCcc());
        this.tfPoblacion.setText(emple.getPoblacion());
        this.cbTipoEmple.setSelectedItem(emple.getTipoempleado());
        this.setModal(true);
        this.setVisible(true);
        this.setLocationRelativeTo(parent);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnAceptar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tfNombre = new javax.swing.JTextField();
        tfApellido1 = new javax.swing.JTextField();
        cbTipoEmple = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        tfApellido2 = new javax.swing.JTextField();
        tfDir = new javax.swing.JTextField();
        tfCCC = new javax.swing.JTextField();
        tfCP = new javax.swing.JTextField();
        tfPoblacion = new javax.swing.JTextField();
        tfDNI = new javax.swing.JTextField();
        btnPermisos = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Modificar Empleado");

        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jLabel1.setText("DNI");

        jLabel2.setText("Tipo empleado");

        jLabel3.setText("Nombre");

        cbTipoEmple.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel4.setText("Dirección");

        jLabel5.setText("Apellido1");

        jLabel6.setText("Apellido2");

        jLabel7.setText("CP");

        jLabel8.setText("Población");

        jLabel9.setText("CCC");

        tfApellido2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfApellido2ActionPerformed(evt);
            }
        });

        tfDir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfDirActionPerformed(evt);
            }
        });

        tfPoblacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfPoblacionActionPerformed(evt);
            }
        });

        tfDNI.setEnabled(false);

        btnPermisos.setText("Añadir Permiso");
        btnPermisos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPermisosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnAceptar)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel8))
                                .addGap(35, 35, 35)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(tfNombre, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(cbTipoEmple, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(tfApellido2, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(tfApellido1)
                                    .addComponent(tfDir)
                                    .addComponent(tfCCC)
                                    .addComponent(tfCP, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(tfPoblacion)
                                    .addComponent(tfDNI)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(0, 92, Short.MAX_VALUE)
                                .addComponent(btnPermisos)
                                .addGap(18, 18, 18)
                                .addComponent(btnCancelar)))
                        .addGap(55, 55, 55))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfDNI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbTipoEmple, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfApellido1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfApellido2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tfDir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(tfCP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(tfCCC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(tfPoblacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAceptar)
                    .addComponent(btnCancelar)
                    .addComponent(btnPermisos)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tfApellido2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfApellido2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfApellido2ActionPerformed

    private void tfDirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfDirActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfDirActionPerformed

    private void tfPoblacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfPoblacionActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfPoblacionActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        String sDNI,sNombre,sApe1,sApe2,sDir, sCP, sCCC, sPobl;
        sDNI=this.tfDNI.getText();
        sNombre=this.tfNombre.getText();
        sApe1=this.tfApellido1.getText();
        sApe2=this.tfApellido2.getText();
        sDir=this.tfDir.getText();
        sCP=this.tfCP.getText();
        sCCC=this.tfCCC.getText();
        sPobl=this.tfPoblacion.getText();
        Tipoempleado tEmple=(Tipoempleado)this.cbTipoEmple.getSelectedItem();
        if(sDNI.length()==9){
            if(tEmple!=null){
                try{
                    int cp=Integer.parseInt(sCP);
                    emple.setNombre(sNombre);
                    emple.setApellido1(sApe1);
                    emple.setApellido2(sApe2);
                    emple.setDireccion(sDir);
                    emple.setCp(cp);
                    emple.setCcc(sCCC);
                    emple.setPoblacion(sPobl);
                    emple.setTipoempleado(tEmple);
                    try{
                        if(permiso!=null && modulo!=null){
                            AsignaId id=new AsignaId(emple.getCodEmpleado(), permiso.getIdPermiso(), modulo.getIdModulo());
                            Asigna asigna=new Asigna(id,emple,modulo,permiso);
                            emple.addAsignas(asigna);
                            new EmpleadosDAO().actualizaEmpleado(emple);
                            JOptionPane.showMessageDialog(this,"Empleado modificado\nPermiso añadido","Información!", JOptionPane.INFORMATION_MESSAGE);
                            this.tfDNI.setText("");
                            this.tfNombre.setText("");
                            this.tfApellido1.setText("");
                            this.tfApellido2.setText("");
                            this.tfDir.setText("");
                            this.tfCP.setText("");
                            this.tfCCC.setText("");
                            this.tfPoblacion.setText("");
                            this.cbTipoEmple.setSelectedIndex(0);
                        }else{
                            new EmpleadosDAO().actualizaEmpleado(emple);
                            JOptionPane.showMessageDialog(this,"Empleado modificado","Información!", JOptionPane.INFORMATION_MESSAGE);
                        }
                    }catch(HibernateException he){
                        JOptionPane.showMessageDialog(this,"Hibernate dice: "+he.getMessage(),"Error de datos!", JOptionPane.ERROR_MESSAGE);
                    } 
                }catch(NumberFormatException ex){
                    JOptionPane.showMessageDialog(this,"Codigo postal no valido","Error de formato!", JOptionPane.ERROR_MESSAGE);
                }
            }else{
                JOptionPane.showMessageDialog(this,"Tipo de empleado no valido","Error de formato!", JOptionPane.ERROR_MESSAGE);
            }
        }else{
            JOptionPane.showMessageDialog(this, "Introduce un dni valido", "Error de formato!", JOptionPane.ERROR_MESSAGE);
        }     
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void btnPermisosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPermisosActionPerformed

        Jd_add_permiso jdp=new Jd_add_permiso(this, permiso, modulo);

    }//GEN-LAST:event_btnPermisosActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnPermisos;
    private javax.swing.JComboBox cbTipoEmple;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField tfApellido1;
    private javax.swing.JTextField tfApellido2;
    private javax.swing.JTextField tfCCC;
    private javax.swing.JTextField tfCP;
    private javax.swing.JTextField tfDNI;
    private javax.swing.JTextField tfDir;
    private javax.swing.JTextField tfNombre;
    private javax.swing.JTextField tfPoblacion;
    // End of variables declaration//GEN-END:variables

     void setPermisos(Permisosempleados permiso, Modulopermisosempleados modulo) {
        this.permiso=permiso;
        this.modulo=modulo;
    }

}
